<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'puorder:poorder:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.parent.id })">
          {{ record.parent.vordercode }}
        </a>
      </template>

      <template #ddate="{ record, column }">
        {{ formatDate(record['parent'][column.dataIndex]) }}
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <listOrderChg @register="registerDrawer2" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsSrmPuorderOrderPoorderListDetails">
  import { unref,onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { poorderDelete,  poorderBListData } from '/@/api/srm/puorder/order/poorder';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import listOrderChg from './listOrderChg.vue';
  import { companyTreeData } from '/@/api/sys/company';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();


  const { t } = useI18n('puorder.poorder');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('采购订单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公司'), 
        field: 'pkCorp',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          immediate: true,
        },
      },
      // {
      //   label: t('公司'),
      //   field: 'unitname',
      //   component: 'Input',
      // },
      {
        label: t('订单编号'),
        field: 'parent.vordercode',
        component: 'Input',
      },
      {
        label: t('订单日期'),
        field: 'parent.dorderdate',
        component: 'RangePicker',
        
      },
      {
        label: t('计划到货'),
        field: 'dplanarrvdate',
        component: 'RangePicker',
        
      },
      {
        label: t('供应商'),
        field: 'parent.custName',
        component: 'Input',
      },
      {
        label: t('采购员'),
        field: 'parent.psnName',
        component: 'Input',
      },
      {
        label: t('采购部门'),
        field: 'parent.deptName',
        component: 'Input',
      },
      {
        label: t('制单人'),
        field: 'parent.createByName',
        component: 'Input',
      },
      
      {
        label: t('订单状态'),
        field: 'parent.puOrderStatus.orderStatus',
        component: 'Select',
        componentProps: {
          dictType: 'srm_order_comfirm_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
     
    ],
    fieldMapToTime: [['dplanarrvdate', ['dplanarrvdate_gte', 'dplanarrvdate_lte']],['parent.dorderdate', ['parent.dorderdate_gte', 'parent.dorderdate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('公司'),
      dataIndex: 'unitname',
      key: 'a.unitname',
      sorter: true,
      width: 230,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('订单编号'),
      dataIndex: 'parent.vordercode',
      key: 'parent.vordercode',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('版本号'),
      dataIndex: 'parent.versionDesc',
      key: 'parent.nversion',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('订单日期'),
      dataIndex: 'parent.dorderdate',
      key: 'parent.dorderdate',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('行号'),
      dataIndex: 'crowno',
      key: 'a.crowno',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('存货编码'),
      dataIndex: 'cinvcode',
      key: 'a.cinvcode',
      sorter: true,
      width: 110,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'cinvname',
      key: 'a.cinvname',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('规格'),
      dataIndex: 'cinvspes',
      key: 'a.cinvspes',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('型号'),
      dataIndex: 'cinvtype',
      key: 'a.cinvtype',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'nordernum',
      key: 'a.nordernum',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('计划到货'),
      dataIndex: 'dplanarrvdate',
      key: 'a.dplanarrvdate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('是否赠品'),
      dataIndex: 'blargess',
      key: 'a.blargess',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('业务流程'),
      dataIndex: 'parent.businame',
      key: 'parent.businame',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('采购组织'),
      dataIndex: 'parent.puname',
      key: 'parent.puname',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('供应商'),
      dataIndex: 'parent.custName',
      key: 'parent.cust_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('合计数量'),
      dataIndex: 'parent.ntotalnum',
      key: 'parent.ntotalnum',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('采购员'),
      dataIndex: 'parent.psnName',
      key: 'parent.psn_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('采购部门'),
      dataIndex: 'parent.deptName',
      key: 'parent.dept_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('制单人'),
      dataIndex: 'parent.createByName',
      key: 'parent.create_by_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('制单时间'),
      dataIndex: 'createDate',
      key: 'parent.create_date',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'ddate',
    },
    {
      title: t('修改人'),
      dataIndex: 'parent.updateByName',
      key: 'parent.update_by_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('修改时间'),
      dataIndex: 'updateDate',
      key: 'parent.update_date',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'ddate',
    },
    {
      title: t('审核人'),
      dataIndex: 'parent.auditByName',
      key: 'parent.audit_by_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('审核时间'),
      dataIndex: 'auditDate',
      key: 'parent.audit_date',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'ddate',
    },
    {
      title: t('订单状态'),
      dataIndex: 'parent.puOrderStatus.orderStatus',
      defaultValue: '待确认',
      key: 'status.order_status',
      dictType: 'srm_order_comfirm_status',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('是否最新版本(Y/N)'),
      dataIndex: 'parent.bislatest',
      key: 'parent.bislatest',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('备注(表头)'),
      dataIndex: 'parent.remarks',
      key: 'parent.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('备注(表体)'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 200,
    actions: (record: Recordable) => [
      {
        icon: 'i-simple-line-icons:eye',
        title: t('查看采购订单'),
        onClick: handleForm.bind(this, { id: record.parent.id }),
        auth: 'puorder:poorder:view',
      },
      {
        // icon: 'i-simple-line-icons:eye',
        title: t('变更历史'),
        label: t('变更历史'),
        onClick: handlePush.bind(this, {...record.parent}),
        auth: 'puorder:poorder:view',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawer2, { openDrawer:openDrawer2 }] = useDrawer();
  const [registerTable, { reload,getForm }] = useTable({
    api: poorderBListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handlePush(record: Recordable) {
    openDrawer2(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await poorderDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  // 时间格式化日期，只保留年月日
  const formatDate = (date: string) => {
    if (!date) return '';
    const dateObj = new Date(date);
    const year = dateObj.getFullYear();
    const month = dateObj.getMonth() + 1;
    const day = dateObj.getDate();
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
  };

  // 默认查询条件
  onMounted(async () => {
    handleCompany();
  });

  async function handleCompany() {
    await getForm().setFieldsValue({
      pkCorp: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      pkCorp: userStore.getProjecte.code,
    });
  }
</script>
